package com.shandao.shangxiu.ui;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.shandao.shangxiu.R;
import com.shandao.shangxiu.base.BaseActivity;
import com.shandao.shangxiu.util.TDevice;
import com.shandao.shangxiu.util.UIHelper;
import com.shandao.shangxiu.widget.CleanableEditText;

/**
 * 找回密码页面
 * @author king
 */
public class ForgetActivity extends BaseActivity {
	 public static final String TAG = ForgetActivity.class.getSimpleName();

    private CleanableEditText etPhone;// 显示发送手机号码
    private EditText etCheckCode;//验证码输入
    private TextView tvCheckCode;//验证码按钮
    private Button btnNext;

    private CountDownTimer timer;//倒计时

    private String checkCode;//验证码
    private String phone;//发送手机号码

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget;
    }

    @Override
    public void initView() {
        etPhone = (CleanableEditText) findViewById(R.id.et_user_phone);
        etCheckCode = (EditText) findViewById(R.id.et_check_code);
        tvCheckCode = (TextView) findViewById(R.id.tv_checkcode);
        btnNext = (Button) findViewById(R.id.btn_next);

        tvCheckCode.setOnClickListener(this);
        btnNext.setOnClickListener(this);
    }

    @Override
    public void initData() {
        setToolbarTitle("忘记密码");
    }

    /**
     * 启动倒计时
     */
    private void startCountDown() {
        tvCheckCode.setEnabled(false);
        timer = new CountDownTimer(60000, 1000) {
            public void onTick(long millisUntilFinished) {
                tvCheckCode.setText(millisUntilFinished / 1000 + "s\n重发验证码");
            }

            @Override
            public void onFinish() {
                tvCheckCode.setText("获取验证码");
                tvCheckCode.setEnabled(true);
            }
        };
        timer.start();
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        if(id==R.id.btn_next){
            next();
        }else if(id==R.id.tv_checkcode){
        	getCheckCode();
        }
    }

    /**
     * 下一步
     */
    private void next() {
        if(!isPrepareForNext()){
            return;
        }
        checkCheckCode();
    }

    /**
     * 验证验证码的正确性
     */
    private void checkCheckCode() {
        UIHelper.showChangeLoginPasswordActivity(this);
    }


    /**
     * 获取验证码
     */
    public void getCheckCode() {
        isPrepareForNet();
        startCountDown();
    }


    private boolean isPrepareForNext() {
        isPrepareForNet();
        checkCode = etCheckCode.getText().toString().trim();
        if (TextUtils.isEmpty(checkCode)){
            showToast("请输入验证码");
            return false;
        }
        return true;
    }


}
